using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;

#if ENABLE_WINMD_SUPPORT
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.Graphics.Imaging;
#endif
public static class UWPFileOpration
{

    /* DocumentsLibrary = 2
  * PicturesLibrary = 7
  * */

    public async static Task SaveFileToLibrary(string FileName, byte[] Bytedata, int foldID, string subDictionary = null, Action<string> action = null)
    {
#if ENABLE_WINMD_SUPPORT
        StorageFolder storageFolder = await KnownFolders.GetFolderForUserAsync(null /* current user */, (KnownFolderId)foldID);
        StorageFile file;
        try
        {
            if (subDictionary == null)
                file = await storageFolder.CreateFileAsync(FileName, CreationCollisionOption.ReplaceExisting);
            else
            {
                StorageFolder subStorageFolder = await storageFolder.CreateFolderAsync(subDictionary, CreationCollisionOption.OpenIfExists);
                file = await subStorageFolder.CreateFileAsync(FileName, CreationCollisionOption.ReplaceExisting);
            }
            await FileIO.WriteBytesAsync(file, Bytedata);
            if (action != null)
            {
                action(FileName);
            }
        }
        catch (Exception ex)
        {

        }
        GC.Collect();
#endif
    }
#if ENABLE_WINMD_SUPPORT
    public static async Task SaveFileToLibrary(string FileName, SoftwareBitmap bitmap, int foldID, string subDictionary = null, Action<string> action = null)
    {
        StorageFolder storageFolder = await KnownFolders.GetFolderForUserAsync(null /* current user */, (KnownFolderId)foldID);
        StorageFile file;
        if (subDictionary == null)
            file = await storageFolder.CreateFileAsync(FileName, CreationCollisionOption.ReplaceExisting);
        else
        {
            StorageFolder subStorageFolder = await storageFolder.CreateFolderAsync(subDictionary, CreationCollisionOption.OpenIfExists);
            file = await subStorageFolder.CreateFileAsync(FileName, CreationCollisionOption.ReplaceExisting);
        }

        using (var outputStream = await file.OpenAsync(FileAccessMode.ReadWrite))
        {
            var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.BmpEncoderId, outputStream);
            encoder.SetSoftwareBitmap(bitmap);
            await encoder.FlushAsync();
            encoder = null;

        }
        if (action != null)
        {
            action(FileName);
        }
        GC.Collect();

}
#endif

    public static async Task<byte[]> ReadFileFromLibrary(string FileName, int foldID, string subFoldName = null, Action<string> action = null)
    {
        byte[] bytes = new byte[] { };
#if ENABLE_WINMD_SUPPORT
        StorageFile file;
        StorageFolder storageFolder = await KnownFolders.GetFolderForUserAsync(null /* current user */, (KnownFolderId)foldID);
        if (subFoldName == null)
            file = await storageFolder.GetFileAsync(FileName) as StorageFile;
        else
        {
            StorageFolder subStorageFolder = await storageFolder.GetFolderAsync(subFoldName);
            file = await subStorageFolder.GetFileAsync(FileName) as StorageFile;
        }

        IRandomAccessStreamWithContentType readstream = await file.OpenReadAsync();
        using (Stream stream = readstream.AsStreamForRead((int)readstream.Size))
        {
            bytes = new byte[stream.Length];
            await stream.ReadAsync(bytes, 0, (int)stream.Length);
        }

        if (action != null)
        {
            action(FileName);
        }
        GC.Collect();
#endif
        return bytes;
    }

    public static async Task<byte[]> ReadFileViaPath(string FilePath, Action<string> action = null)
    {
        using (var fileStream = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
        {
            byte[] bytes = new byte[fileStream.Length];
            await fileStream.ReadAsync(bytes, 0, (int)fileStream.Length);

            return bytes;
        }

        if (action != null)
        {
            action(Path.GetFileName(FilePath));
        }

        GC.Collect();
    }

}
